﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CMSWindowsFormsApp2
{
    public partial class JobDetailsForm : Form
    {
        private SqlConnection con;
        private SqlDataAdapter adapter;
        private DataSet jobDetailsDataSet;
        private BindingSource bindingSource;

        public JobDetailsForm()
        {
            InitializeComponent();
        }

        private void txtCarNo_Leave(object sender, EventArgs e)
        {
           
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try {
                if (txtCarNo.Text.Length < 6) {
                    MessageBox.Show("Please Specify a valid Car Number");
                    txtCarNo.Focus();
                    return;
                }
                if (Convert.ToInt32(txtWorkerId.Text) < 1) {
                    MessageBox.Show("Please Specify a valid Worker Id");
                    txtWorkerId.Focus();
                    return;
                }
                if (Convert.ToDateTime(dtJobDate.Text) > DateTime.Today) {
                    MessageBox.Show("Please Specify a valid Date");
                    dtJobDate.Focus();
                    return;
                }

                //update record 

                bindingSource.EndEdit();
                adapter.Update(jobDetailsDataSet,"tblJobDetails");
                MessageBox.Show("Record Updated Successfully!");

            }
            catch (Exception ex) {
                MessageBox.Show("Error :"+ex.Message);
            }
            
            

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)

        {
            if (bindingSource == null){
                bindingSource = new BindingSource();
            }
            if (bindingSource.DataSource==null) {
                MessageBox.Show("Data Source is empty. Load data First.");
                return;
            }
            bindingSource.DataSource = jobDetailsDataSet.Tables["tblJobDetails"];
            bindingSource.AddNew();
            UpdatePositionLabel();

        }

        private void JobDetailsForm_Load(object sender, EventArgs e)
        {

        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            con = new SqlConnection("Data Source=DESKTOP-SR1MKQK;Initial Catalog=CMSDATABASE;Integrated Security=True");
            adapter = new SqlDataAdapter("Select * from tblJobDetails", con);
            jobDetailsDataSet = new DataSet();
            SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(adapter);
            adapter.Fill(jobDetailsDataSet, "tblJobDetails");

            bindingSource = new BindingSource { DataSource = jobDetailsDataSet.Tables["tblJobDetails"] };
            
           
            foreach (Control control in this.Controls) {
                if (control is TextBox txt && txt.Name.StartsWith("txt")) {
                    txt.DataBindings.Clear();
                    string columnName = txt.Name.Substring(3);//remove "txt"
                    txt.DataBindings.Add("Text", bindingSource, columnName);
                }
            }
            UpdatePositionLabel();//create a methods
        }
        private void UpdatePositionLabel()
        {
            if (bindingSource.Count > 0) {
                textPosition.Text = $"{bindingSource.Position + 1}  of{ bindingSource.Count}";
            } else {
                textPosition.Text = "0 of 0";
            }  
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (bindingSource.Position < bindingSource.Count - 1) {
                bindingSource.MoveNext();
                UpdatePositionLabel();
            }
        }
             

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (bindingSource.Position > 0) {
                bindingSource.MovePrevious();
                UpdatePositionLabel();
            }
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            bindingSource.MoveFirst();
            UpdatePositionLabel();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            bindingSource.MoveLast();
            UpdatePositionLabel();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (bindingSource.Count>0) {
                bindingSource.RemoveCurrent();
                adapter.Update(jobDetailsDataSet, "tblJobDetails");
                MessageBox.Show("Record Deleted Successfully!");
                UpdatePositionLabel();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            bindingSource.CancelEdit();
            UpdatePositionLabel();
        }

        private void btnCancleAll_Click(object sender, EventArgs e)
        {
            jobDetailsDataSet.RejectChanges();
            UpdatePositionLabel();
            
        }
    }
}
